package org.abettor.pushbox.activity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.db.PushBoxMapBeanManage;
import org.abettor.pushbox.map.Boxmap;
import org.abettor.pushbox.map.BoxmapUtil;
import org.abettor.pushbox.util.Resource;
import org.abettor.pushbox.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;

public class ClassicPushBoxActivity extends AbstractPushBoxActivity {
	private final int FULL_UNFULL_SCREEN_ID = Menu.FIRST;// 全屏
	private final int RESTART_GAME = Menu.FIRST + 1;// 重新开始
	private final int PRE_STEP = Menu.FIRST + 2;// 上一步的操作
	private final int SHOCK_UNSHOCK_ID = Menu.FIRST + 3;// 打开按键反馈
	private PushBoxMapBeanManage pushBoxManage;
	private int map_id = -1;// 地图的id
	private Handler mHandler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		pushBoxManage = new PushBoxMapBeanManage(this);
		super.onCreate(savedInstanceState);
	}

	// 读取地图文件
	private byte[] readFile(int mapId) throws IOException {
		int missionId = pushBoxManage.getMissionId(mapId);
		InputStream bin = getResources().openRawResource(
				Resource.getMapResourceByMission(missionId));
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		int v = 0;
		while ((v = bin.read()) != -1) {
			bo.write(v);
		}
		byte[] buf = bo.toByteArray();
		bin.close();
		bo.close();
		return buf;
	}

	/**
	 * 创建系统菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		if (m_pushConfig.getFullScreenOn() == 0) {// 是否打开全屏
			menu.add(0, FULL_UNFULL_SCREEN_ID, 0, R.string.full_screen)
					.setIcon(R.drawable.full_screen_on);
		} else {
			menu.add(0, FULL_UNFULL_SCREEN_ID, 0, R.string.un_full_screen)
					.setIcon(R.drawable.full_screen_off);
		}
		menu.add(0, RESTART_GAME, 1, R.string.restart_game).setIcon(
				R.drawable.reset);
		menu.add(0, PRE_STEP, 2, R.string.pre_step).setIcon(R.drawable.go_back);
		if (m_pushConfig.getShakeOn() == 0) {
			menu.add(0, SHOCK_UNSHOCK_ID, 3, R.string.shock_open).setIcon(
					R.drawable.shaken_on);
		} else {
			menu.add(0, SHOCK_UNSHOCK_ID, 3, R.string.shock_close).setIcon(
					R.drawable.shaken_off);
		}
		return result;
	}

	/**
	 * 系统菜单执行的操作
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case FULL_UNFULL_SCREEN_ID:// 全屏
			fullScreen();
			break;
		case RESTART_GAME:// 重新开始游戏
			restart();
			break;
		case PRE_STEP:// 上一步操作
			revert();
			break;
		case SHOCK_UNSHOCK_ID:// 打开按键反馈
			onShock();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem full = menu.findItem(FULL_UNFULL_SCREEN_ID);
		if (m_pushConfig.getFullScreenOn() == 0) {
			full.setIcon(R.drawable.full_screen_on);
			full.setTitle(R.string.full_screen);
		} else {
			full.setIcon(R.drawable.full_screen_off);
			full.setTitle(R.string.un_full_screen);
		}
		MenuItem shock = menu.findItem(SHOCK_UNSHOCK_ID);
		if (m_pushConfig.getShakeOn() == 0) {
			shock.setIcon(R.drawable.shaken_on);
			shock.setTitle(R.string.shock_open);
		} else {
			shock.setIcon(R.drawable.shaken_off);
			shock.setTitle(R.string.shock_close);
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	protected Boxmap initTheBoxMap() {
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		map_id = extras.getInt("_id");// 获得数据的id
		byte[] buf = null;
		try {
			buf = readFile(map_id);
		} catch (IOException e) {
		}
		m_map = BoxmapUtil.paraArrayToMap(buf);// 生成地图
		return m_map;
	}

	/**
	 * 地图成功时的操作
	 */
	@Override
	protected void successOperation() {
		Thread thread = new Thread() {
			@Override
			public void run() {
				int missionId = pushBoxManage.getMissionId(map_id);
				int status = pushBoxManage.getMissionStatus(map_id);
				if (status == 1) {
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							AlertDialog.Builder builder = new AlertDialog.Builder(
									ClassicPushBoxActivity.this);
							AlertDialog dialog = builder.create();
							dialog.setTitle(R.string.push_map_success);
							dialog.setMessage(ClassicPushBoxActivity.this
									.getText(R.string.classic_map_re_resolved));
							dialog.setButton(DialogInterface.BUTTON_POSITIVE,
									ClassicPushBoxActivity.this
											.getText(android.R.string.ok),
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
										}
									});
							dialog.show();
						}
					});
					return;
				}
				// 进行常规的处理
				pushBoxManage.updatePushBoxMapBeanSuccess(map_id);// 更新本地图
				if (missionId == 80) {// 最后一关
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							AlertDialog.Builder builder = new AlertDialog.Builder(
									ClassicPushBoxActivity.this);
							AlertDialog dialog = builder.create();
							dialog.setTitle(R.string.push_map_success);
							dialog.setMessage(ClassicPushBoxActivity.this
									.getText(R.string.classic_map_allresolved));
							dialog.setButton(DialogInterface.BUTTON_POSITIVE,
									ClassicPushBoxActivity.this
											.getText(android.R.string.ok),
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
										}
									});
							dialog.show();
						}

					});
				} else {
					final int nxtId = pushBoxManage
							.getIdByMission(missionId + 1);
					pushBoxManage.updatePushBoxMapBeanToPush(nxtId);// 更新下一关可推
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							AlertDialog.Builder builder = new AlertDialog.Builder(
									ClassicPushBoxActivity.this);
							AlertDialog dialog = builder.create();
							dialog.setTitle(R.string.push_map_success);
							dialog.setMessage(ClassicPushBoxActivity.this
									.getText(R.string.classic_map_resolved));
							dialog.setButton(DialogInterface.BUTTON_POSITIVE,
									ClassicPushBoxActivity.this
											.getText(android.R.string.ok),
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											Intent in = new Intent();
											Bundle extras = new Bundle();
											extras.putInt("_id", nxtId);
											in.putExtras(extras);
											in.setClass(
													ClassicPushBoxActivity.this,
													ClassicPushBoxActivity.class);
											startActivity(in);
											ClassicPushBoxActivity.this
													.finish();
										}
									});
							dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
									ClassicPushBoxActivity.this
											.getText(android.R.string.cancel),
									new DialogInterface.OnClickListener() {
										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
										}
									});
							dialog.show();
						}
					});
				}
			}
		};
		thread.start();
	}

	@Override
	protected String getStepFileName() {
		int missionId = pushBoxManage.getMissionId(map_id);
		String filepath = Config.getClassicBasePath() + File.separator
				+ String.valueOf(missionId) + ".step";
		return filepath;
	}
}
